package com.xb.xbpm.controller;

import com.xb.xbpm.service.ISiteService;
import com.xb.xbpm.utils.EsUtils;
import com.xb.xbpm.utils.HttpClientUtil;
import com.xb.xbpm.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Auther: zhangzhen
 * @Date: 2018/10/22 11:06
 * @Description:
 */
@RestController
@RequestMapping("advcheat")
public class AdvCheatController {



    @Autowired
    EsUtils esUtils;
    @Autowired
    ISiteService siteService;


    /**
     * 小说——获取作弊列表
     * @param request
     * @return
     */
    @RequestMapping("/getList")
    public Map<String, Object> getList(HttpServletRequest request){
        ModelMap map=new ModelMap();
        List<Map<String, Object>> cheatList=new ArrayList<Map<String,Object>>();
//        ListPageUtil<Map<String, Object>> listPageUtil = null;
        List<String> datelist=null;
        String startDate="";
        String endDate="";
        String date= request.getParameter("date");
        String userid= request.getParameter("userid");

        if(Tools.notEmpty(date)){//日期不为空
            String [] strs=date.split(",");
            startDate=strs[0].replaceAll("-","");
            endDate=strs[1].replaceAll("-","");
        }else{//默认当天
            startDate=Tools.date2Str(new Date(),"yyyyMMdd");
        }
        try {
            //拼接索引
            String index="";
            if(Tools.isEmpty(endDate) || startDate.equals(endDate)){
                //一天的
                index="lm_"+startDate;
            }else{
                //时间段内的所有索引值
                DateFormat format1 = new SimpleDateFormat("yyyyMMdd");
                Date start=null;
                Date end=null;
                start = format1.parse(startDate);
                end= format1.parse(endDate);
                datelist= getBetweenDates2(start,end);
                datelist.add(endDate);
                for (String s: datelist ) {
                    index+="lm_"+s+",";
                }
            }
            //查找作弊列表
            cheatList= esUtils.getCheatList3(index);

//            Integer pageNum =  request.getParameter("pageNum") == null?1:Integer.parseInt( request.getParameter("pageNum"));
//            Integer pageSize = request.getParameter("pageSize") == null?100:Integer.parseInt( request.getParameter("pageSize"));

//            listPageUtil = new ListPageUtil<>(cheatList,pageNum, pageSize);
//            cheatList = cheatList.subList((pageNum-1) * pageSize, pageNum * pageSize);
            if(cheatList.size()>200){
                cheatList=cheatList.subList(0,200);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
//        for (Map<String,Object> map1:cheatList){
//            String stats=getStatsBySid(map1.get("sid").toString());
//            map1.put("stats",stats);
//        }
//        listPageUtil.getData().parallelStream().forEach(x->{
//            String stats=getStatsBySid(x.get("uid").toString());
//            x.put("stats",stats);
//        });
        cheatList.parallelStream().forEach(x->{
            String stats=getStatsByUid(x.get("uid").toString());
            x.put("stats",stats);
        });
//        map.put("cheatList",listPageUtil);
        map.put("cheatList",cheatList);
        return map;
    }

    private static List<String> getBetweenDates2(Date start, Date end) {
        DateFormat format1 = new SimpleDateFormat("yyyyMMdd");
        List<String> result = new ArrayList<String>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        //tempStart.add(Calendar.DAY_OF_YEAR, 1);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        while (tempStart.before(tempEnd)) {
            result.add(format1.format(tempStart.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

    private  String getStatsByUid(String uid) {
        String result = "";
        try {
            Map<String, String> param = new HashMap<String, String>();
            param.put("uid", uid);
            result = HttpClientUtil.doGet("http://t.xinba.com/tung_splinter_status", param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

}
